<?php
@session_start();
$ROOT = "../../../";
require_once ($ROOT . 'config/config.php');

class PublicationIndex{	
	private $db;
	private $post;
	private $smarty;
	
	function __construct($db, $post, $smarty){
		$this -> db 	= $db;
		$this -> post 	= $post;
		$this -> smarty = $smarty;
	}
	
	
	function insert(){
		$id_user 		= $this -> post['id_user'];
		$reg_date 		= "";
		$title 			= $this -> post['title'];
		$description 	= $this -> post['description'];
		$subcategory 	= $this -> post['subcategory'];
		$response	 	= $this -> post['response'];
		$ini_date	 	= $this -> post['ini_date'];
		$end_date 		= $this -> post['end_date'];
				
		$Publication = new Publication("",$id_user, $reg_date, $title, $description, $subcategory, $response, '', '');
		
		if($description != "" && $description != " " && $title != "" && $title != " "){
			
			$result = $Publication -> insert($this -> db,$Publication);
		}
		echo $result;
	}
	
	function update(){
		$id	 		 	= $this -> post['id'];
		$id_user 		= $this -> post['id_user'];
		$reg_date 		= "";
		$title 			= $this -> post['title'];
		$description 	= $this -> post['description'];
		$subcategory 	= $this -> post['subcategory'];
		$response	 	= $this -> post['response'];
		$ini_date	 	= $this -> post['ini_date'];
		$end_date 		= $this -> post['end_date'];
		
		if($description != "" && $description != " " && $title != "" && $title != " " && $subcategory != "" && $subcategory != " "){
			$Publication = new Publication($id, $id_user, $reg_date, $title, $description, $subcategory, $response, $ini_date, $end_date);
			
			$result = $Publication -> update($this -> db,$Publication);
		}
		echo $result;
	}
	
	function delete(){
		$id		= $this -> post['id'];
		$Publication = new Publication($id,"");
		
		$result = $Publication -> delete($this -> db,$Publication);
		echo $result;
	}
	
	
	function display(){
		$subcategory = Subcategory::getList($this -> db);
		$id_user = $_SESSION['Kiliwuser'];
		$result = Publication::getMyList($this -> db,$id_user);
		$this -> smarty ->assign('id_user' , $id_user);
		$this -> smarty ->assign('subcategory' , $subcategory);
		$this -> smarty ->assign('listPublication' , $result);
			
		$this -> smarty ->display('../templates/publication/index.tpl');
	}
	
	function main($db,$smarty,$post){
		$method = $post['method'];
		$index = new PublicationIndex($db,$post,$smarty);
		
		switch ($method){
			case 'insert':
				$index -> insert();		
				break;
			case 'update':
				$index -> update();		
				break;
			case 'delete':
				$index -> delete();		
				break;
			
			default:
				$index -> display();
		}
	}
}

PublicationIndex::main($db,$smarty,$_POST);
?>